import ElementPlus from "element-plus";
import * as ElementPlusIconsVue from "@element-plus/icons-vue";
import "element-plus/dist/index.css";
import zhCn from 'element-plus/es/locale/lang/zh-cn'

import xcore from "xcore/index"
import $cmpnt, { VueComponent } from "xcore/cmpnt"
import {emitReady} from "xcore/data"
import type { App } from "vue";
import 'dayjs/locale/zh-cn'

// 定义 install 方法，接收 Vue 作为参数。如果使用 use 注册插件，则所有的组件都将被注册
let xpc = {
    installed: false,
    install:function (app: App, options?: any) {
        if (this.installed) return
        this.installed = true

        const modules = import.meta.glob("./cmpnts/**/*.ts", { eager: true });
        $cmpnt.addModules(Object.values(modules)); 

        const components = import.meta.glob<{ default: VueComponent }>(["./cmpnts/**/*.vue"], { eager: true })
        $cmpnt.addComponents(components)
    
        app.use(ElementPlus, { size: "small",locale:zhCn });//zIndex: 3000
        for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
            app.component(key, component);
        }


        app.use(xcore)
        if(!options || !options.editable){
            emitReady()
             return
        }
       

        if(options && options.editable){
           import('xcore/designer').then((designerModule:any) => {
            let designer = designerModule.default.get()
            designer.addModules(Object.values(modules));
            designer.install(app)
         });
        }
    }
}

export default xpc